package com.web.base.user.util;

import com.blue.integration.serve.Bucket;
import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.base.user.model.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年04月16日
 */
public class UserConvertor
{
	private UserConvertor()
	{
	}
	
	public static User toUser(UserRequest request)
	{
		if (request == null)
			return null;
		
		User user = new User();
		user.setId(request.getId());
		user.setName(request.getName());
		user.setPassword(request.getPassword());
		user.setMobile(request.getMobile());
		user.setWxUnionId(request.getWxUnionId());
		user.setNickname(request.getNickname());
		user.setPic(request.getPic());
		user.setEmail(request.getEmail());
		user.setCreateTime(request.getCreateTime());
		user.setLoginTime(request.getLoginTime());
		user.setRole(request.getRole());
		user.setAuth(request.getAuth());
		user.setWechatId(request.getWechatId());
		user.setWxOpenId(request.getWxOpenId());
		return user;
	}
	
	public static UserResponse toResponse(User user, StorageClient storageClient)
	{
		if (user == null)
			return null;
		
		UserResponse response = new UserResponse();
		response.setId(user.getId());
		response.setName(user.getName());
		response.setPassword(user.getPassword());
		response.setMobile(user.getMobile());
		response.setWxUnionId(user.getWxUnionId());
		response.setNickname(user.getNickname());
		response.setPic(user.getPic());
		response.setEmail(user.getEmail());
		response.setCreateTime(user.getCreateTime());
		response.setLoginTime(user.getLoginTime());
		response.setRole(user.getRole());
		response.setAuth(user.getAuth());

		response.setPicUrl(storageClient.getUrl(Bucket.FILE, response.getPic()));
		response.setRoleNames(user.getRoleNames());
		return response;
	}
	
	public static List<UserResponse> toResponse(List<User> list, StorageClient storageClient)
	{
		List<UserResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (User user : list)
		{
			responseList.add(toResponse(user, storageClient));
		}
		return responseList;
	}
	
	public static Map<Integer, UserResponse> toResponse(Map<Integer, User> map, StorageClient storageClient)
	{
		Map<Integer, UserResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<Integer, User> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue(), storageClient));
		}
		return responseMap;
	}
	
	public static Page<UserResponse> toResponse(Page<User> page, StorageClient storageClient)
	{
		if (page == null)
			return null;
		
		Page<UserResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList(), storageClient));
		return responsePage;
	}
	
}
